#include "udp.h"
#include "mainwindow.h"
#include <cmath>
#include <QDebug>
#define PI 3.1415927
//UdpClient::UdpClient(MainWindow *parent)

#include "myhelper.h"

#define _TIME_ qPrintable(QDateTime::currentDateTime().toString("hh:mm:ss"))

UdpClient::UdpClient()
{
 //   this->parent = parent;

    init();

    m_bRead=false;

    udpSocket=0;
    port = 1999;
    udpSocket = new QUdpSocket(this);
    udpSocket->bind(port);
    connect(udpSocket, SIGNAL(readyRead()),this, SLOT(dataReceived()));

//    qDebug()<<sizeof(ETC_S)<<" "<<sizeof(ETC_D)<<" "<<sizeof(unsigned short)<<" "<<sizeof(double);
}
void UdpClient::init()
{
    watchDog = new QTimer;
    m_iCTPT=0;
    m_dblSin=0;
    m_dblCos=0;
    m_iTestedPoints=0;
    m_iTestedPoints_bc=0;
    m_iTestedZhouQi=0;
    m_iTestedZhouQi_bc=0;
    m_bSamplingPoints=false;
    m_iShuZiLiangMax = 0;
    m_iShuZiLiangMax_Temp = 0;
    m_dblMaxPoint=0;
    m_dblYouXiaoZhi=0;		// 互感器基波有效值
    m_dblXW=0;				// 互感器所示相位

    m_dblYouXiaoZhi_bc=0;		// 互感器基波有效值
    m_dblXW_bc=0;				// 互感器所示相位
    m_iShuZiLiangMax_bc=0;			// 数字量最大值
    m_iShuZiLiangMax_Temp_bc=0;		// 数字量最大值_临时
    m_dblMaxPoint_bc=0;
    m_iZhouQi=0;
    m_iZhouQi_bc=0;
    m_dblSin_bc=0;
    m_dblCos_bc=0;
    m_dblPL=50;

    m_bRun=false;
    m_isOK=false;
    m_bJQ=false;
    bzxs=0;
    bcxs=0;
    smpcnt=-1;
    mt=0;
    yanshi=0;
    jgMax=0;
    jgMin=0;
    bwAll=0;
    bwLost=0;
    bwError=0;
    bcIsTest=false;
    bzOver=false;
    bcOver=false;
    memset(m_tempBz,0,sizeof(m_tempBz));
    memset(m_tempBc,0,sizeof(m_tempBc));

    harmonicOrder = 1;
    delay_ft3_in_packet = 0;
    delay_ft3_zero = 0;
}

UdpClient::~UdpClient()
{
    this->udpSocket->deleteLater();
    quit();
    wait();
}
void UdpClient::stopRun()
{
    if (m_bRun)
    {
        m_bRun=false;
        this->udpSocket->deleteLater();
        quit();
        wait();
    }
}

void UdpClient::run()
{
    /*
    m_bSamplingPoints=false;
    m_bRun=true;
    udpSocket = new QUdpSocket;
    connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
    udpSocket->bind(port);
    exec();
    */
}

void UdpClient::dataReceived()
{
    while (udpSocket->hasPendingDatagrams()) 
    {
	 	QByteArray datagram;
	 	datagram.resize(udpSocket->pendingDatagramSize());
	 	udpSocket->readDatagram(datagram.data(), datagram.size());

         qDebug() << "!!!!!!!!!!!!!!!!!!!!!!" << datagram.size();

        char *ch;
        ch=datagram.data();

        if (datagram.size()==56)
        {
            QString stra(datagram.data());
            if (stra.left(6)=="sanwei")
            {
                m_bSt=true;
                m_bRead=true;
                m_bSamplingPoints=false;
            }
            return;
        }
        if(!m_bRead)
        {
            init();
            m_bJQ=false;
            m_bSt=false;
            return;
        }
   //     if (datagram.size()<1000)
   //         return;
        QString stra(datagram.data());
        if (stra.left(6)!="sanwei")
            return;


        char flag= ch[6] ;      //标志位
        if (flag==0x0)
        {
            dataProcess(ch);
//            QString str(ch);
//            qDebug() << _TIME_ << str;
        }
        else if (flag==0x1)
            delayProcess(ch);
        else if (flag==0x2)
            dsProcess(ch);
        else if (flag==0x3)
            ssProcess(ch);
        else if (flag== 'a')
            packetAnalyzer(ch);

        /*
        m_dblYouXiaoZhi_bc=datagram.size();
        m_dblXW_bc=2.0;
        m_dblPL=50.0;
        emit sigUdpDataIsOK();
        */

    }
}
void UdpClient::dsProcess(char* ch)
{

    ETC_Delay *etc_duishi=(ETC_Delay*)ch;
    QString str(etc_duishi->de_name);            //内部标识符

    m_dblDs = 0.0;

    if (str.length()>=6)
    {
        str=str.left(6);
        if (str!="sanwei")
            return;
    }
    else
        return;

    m_dblDs = etc_duishi->delay;

    qDebug()<<"data recvd:"<<m_dblDs;

    if(m_dblDs < 0)
    {
        m_dblDs *= -1;
    }

    m_dblDs = m_dblDs / 10.000;

    if(m_dblDs > max_dblDs)
    {
            max_dblDs = m_dblDs;
    }

//    qDebug()<<"ds: "<<m_dblDs<<" max:"<<max_dblDs;
}

void UdpClient::packetAnalyzer(char* ch)
{
    packet_error *pkt_error=(packet_error*)ch;

    yanshi=0;
    jgMax=0;
    jgMin=0;
    bwAll=pkt_error->total;
    //bwLost=pkt_error->lost;
    bwLost = 0;
    bwError=pkt_error->disorder;
    m_dblDs=pkt_error->delay / 100.0;
    delay_specified = pkt_error->delay_defined;

    qDebug()<<"delay_defined:"<<pkt_error->delay_defined;

    error1 = pkt_error->error_1;
    error2 = pkt_error->error_2;
    error3 = pkt_error->error_4;
    error4 = pkt_error->error_6;

//    qDebug()<< "new result is coming";
//    qDebug()<< "error1"<<pkt_error->error_1;
//    qDebug()<< "error2"<<pkt_error->error_2;
//    qDebug()<< "error3"<<pkt_error->error_4;
//    qDebug()<< "error4"<<pkt_error->error_6;
//    qDebug()<< "total"<<pkt_error->total;
//    qDebug()<< "lost"<<pkt_error->lost;
//    qDebug()<< "disorder"<<pkt_error->disorder;

    sigUdpDataShow();
}

void UdpClient::ssProcess(char* ch)
{
    ETC_Duishi *etc_duishi=(ETC_Duishi*)ch;
    QString str(etc_duishi->de_name);            //内部标识符
    if (str.length()>=6)
    {
        str=str.left(6);
        if (str!="sanwei")
            return;
    }
    else
        return;
    m_dblSs=etc_duishi->dTime;
 //   qDebug()<<"ss: "<<m_dblSs;
}

void UdpClient::delayProcess(char* ch)
{
    ETC_Delay *etc_delay=(ETC_Delay*)ch;
    QString str(etc_delay->de_name);            //内部标识符
    if (str.length()>=6)
    {
        str=str.left(6);
        if (str!="sanwei")
            return;
    }
    else
        return;
//    jgMin=10000,jgMax=0,jgAvg=0;
    yanshi=etc_delay->delay;
    jgMax=etc_delay->jgMax;
    jgMin=etc_delay->jgMin;
    bwAll=etc_delay->bwAll;
    bwLost=etc_delay->bwLost;
    bwError=etc_delay->bwError;

    sigUdpDataShow();
}

void UdpClient::dataProcess(char * ch)
{
    static int computation_is_ok = 0; // give more time to harmonic related computation

    ETC_S *etc_s=(ETC_S*)(ch);

    QString str(etc_s->de_name);            //内部标识符
    if (str.length()>=6)
    {
        str=str.left(6);
        if (str!="sanwei")
            return;
    }
    else
        return;
    int cnum;
    testFlag=(int)etc_s->ETC_DATA.kind;
    if (testFlag==1)               ///*数据类型  1表示标准通道，2表示被测通道，3表示ft3数据*/
    {
        m_iBzFdbs=(int)etc_s->ETC_DATA.ex[0];
        m_iZhouQi=(int)etc_s->ETC_DATA.cycle;
        bzxs=(double)etc_s->ETC_DATA.xs;
        cnum=(int)etc_s->ETC_DATA.cycle_num;
        m_dblMaxPoint=etc_s->ETC_DATA.sample_rate/50;
        if (smpcnt==-1)
            smpcnt=cnum;
        else
        {
            if (smpcnt==9)
                smpcnt=0;
            else
                smpcnt++;
            if (smpcnt!=cnum)
            {
                qDebug()<<smpcnt;
                smpcnt=cnum;
            }

        }
    }
    else
    {
        m_iBcFdbs=(int)etc_s->ETC_DATA.ex[0];
        m_iZhouQi_bc=(int)etc_s->ETC_DATA.cycle;
        bcxs=(double)etc_s->ETC_DATA.xs;
        m_dblMaxPoint_bc=etc_s->ETC_DATA.sample_rate/50;
    }

    if(! m_bSamplingPoints && cnum==0)
    {
        m_bSamplingPoints = true;
        m_iShuZiLiangMax = m_iShuZiLiangMax_Temp;
    //    parent->jsWc(); //计算误差
        m_iShuZiLiangMax_Temp = 0;
        m_iTestedPoints = 0;
        m_iTestedPoints_bc = 0;
        m_iTestedZhouQi=0;
        m_iTestedZhouQi_bc=0;

        m_iShuZiLiangMax_bc = m_iShuZiLiangMax_Temp_bc;
        m_iShuZiLiangMax_Temp_bc = 0;
    }    
    iShuZiLiang=0;

    if (!m_bJQ)
    {
        m_bJQ=true;
    }

    if (testFlag==0)
    {
        bcOver=true;
        bcIsTest=false;
    }
    else if (testFlag==1)
    {
        qDebug()<<"bz"<<etc_s->ETC_DATA.cycle_num<<m_dblMaxPoint;

        for(int i=0;i< m_dblMaxPoint;i++)              //标准数据
        {
            iShuZiLiang =(double)etc_s->ETC_DATA.data[i]*bzxs + 0.000001;     //标准数据
            m_BzArray[m_iTestedPoints]=iShuZiLiang;

            //qDebug()<<"bz,sample value"<<iShuZiLiang;
            double iTemp = std::fabs(iShuZiLiang);
            if(iTemp > m_iShuZiLiangMax_Temp)
                m_iShuZiLiangMax_Temp = iTemp;
            m_iTestedPoints++;
            if (iSuanFa==0)
            {
                m_dblSin += 2 / (double)m_dblMaxPoint * ((double)iShuZiLiang) * std::sin(2 * PI * (m_iTestedPoints-1) / m_dblMaxPoint);
                m_dblCos += 2 / (double)m_dblMaxPoint * ((double)iShuZiLiang) * std::cos(2 * PI * (m_iTestedPoints-1) / m_dblMaxPoint);
            }
        }

        if(m_iTestedZhouQi < m_iZhouQi -1)//etc_s->ETC_DATA.cycle
        {
                m_iTestedZhouQi +=1;
        }
        else
        {
                m_iShuZiLiangMax = m_iShuZiLiangMax_Temp;
                memcpy(m_tempBz,m_BzArray,sizeof(m_BzArray));
//                if (iSuanFa==0)
//                {
//                    m_dblYouXiaoZhi = std::sqrt(m_dblSin * m_dblSin + m_dblCos * m_dblCos)
//                                                / std::sqrt(2) / (double)m_iZhouQi;
//                    m_dblXW = GetXiangJiao(m_dblSin, m_dblCos) * 60;
//                }
//                else if (iSuanFa==2)
//                {
//                    harmonic(m_tempBz,harmonicOrder,m_dblMaxPoint*m_iZhouQi,m_dblMaxPoint*50,0);
//                }
                if(computation_is_ok == 0)
                {
                    harmonic(m_tempBz,harmonicOrder,m_dblMaxPoint*m_iZhouQi,m_dblMaxPoint*50,0);

                    if (!bcIsTest)
                    {
                        if(harmonicOrder < 5)
                            computation_is_ok = 0;
                        else if(harmonicOrder < 10)
                            computation_is_ok = 3;
                        else
                            computation_is_ok = harmonicOrder /3;

                        emit sigUdpDataIsOK();

                        qDebug()<<"bz, computation_is_ok";
                    }
                }
                else if (!bcIsTest)
                {
                    computation_is_ok --;

                    qDebug()<<computation_is_ok;
                }

          //      harmonic(m_BzArray,1,m_dblMaxPoint*m_iZhouQi,m_dblMaxPoint*50,0) ;  //s 所有的点 M：谐波次数（可以不用）len smrate*zhouqi fs smrate*50

            m_isOK=true;
          //   emit sigUdpDataIsOK();
            m_dblSin=0;
            m_dblCos=0;
       //     m_iTestedPoints=0;
          //  m_iTestedPoints_bc = 0;
            m_iTestedZhouQi=0;
          //  m_dblSin_bc=0;
          //  m_dblCos_bc=0;
         //   m_iTestedZhouQi_bc=0;
            m_bSamplingPoints=false;

        }
    }
    else if ((testFlag==2)||(testFlag==3))
    {
        qDebug()<<"bc"<<etc_s->ETC_DATA.cycle_num<<m_dblMaxPoint_bc;

        bcIsTest=true;
        for(int i=0;i< m_dblMaxPoint_bc;i++)           ////被测数据
        {
            iShuZiLiang =(double)etc_s->ETC_DATA.data[i]*bcxs + 0.000001;
            m_BcArray[m_iTestedPoints_bc]=iShuZiLiang;

            //qDebug()<<"bc,sample value"<<iShuZiLiang<<bcxs;

            double iTemp = std::fabs(iShuZiLiang);
            if(iTemp > m_iShuZiLiangMax_Temp_bc)
                m_iShuZiLiangMax_Temp_bc = iTemp;
            m_iTestedPoints_bc++;
            if (iSuanFa==0)
            {
                m_dblSin_bc += 2 / (double)m_dblMaxPoint_bc * ((double)iShuZiLiang) * std::sin(2 * PI * (m_iTestedPoints_bc-1) / m_dblMaxPoint_bc);
                m_dblCos_bc += 2 / (double)m_dblMaxPoint_bc * ((double)iShuZiLiang) * std::cos(2 * PI * (m_iTestedPoints_bc-1) / m_dblMaxPoint_bc);
            }
        }

        if(m_iTestedZhouQi_bc < m_iZhouQi_bc - 1)
        {
                m_iTestedZhouQi_bc +=1;
/*
                if (m_iTestedZhouQi_bc==1)
                {
                    m_dblYouXiaoZhi_bc = std::sqrt(m_dblSin_bc * m_dblSin_bc + m_dblCos_bc * m_dblCos_bc)
                                                / std::sqrt(2) ;
                    qDebug()<<"bcyxz:"<<m_dblYouXiaoZhi_bc<<" "<<bcxs<<" "<<m_dblSin_bc<<" "<<m_dblCos_bc;
                }*/

        }
        else
        {
            if(testFlag==3)
                ft3JianXi(etc_s->ETC_DATA.ft3_config,(int)etc_s->ETC_DATA.ft3_freq);

            if(m_dblMaxPoint_bc == 0)
            {
                m_iShuZiLiangMax = 0.1;
                memset(m_tempBc, 0x00, sizeof(m_tempBc));

                m_dblXW_bc = m_dblYouXiaoZhi_bc = 0;

                myHelper::ShowMessageBoxError("没有被测采样值，可能是 FT3 没有接入。");

                emit sigUdpDataIsOK();

                return;
            }
            else
            {
                m_iShuZiLiangMax = m_iShuZiLiangMax_Temp;

            //s 所有的点 M：谐波次数（可以不用）len smrate*zhouqi fs smrate*50
                memcpy(m_tempBc,m_BcArray,sizeof(m_BcArray));
            }
//                if (iSuanFa==0)
//                {
//                    m_dblYouXiaoZhi_bc = std::sqrt(m_dblSin_bc * m_dblSin_bc + m_dblCos_bc * m_dblCos_bc)
//                                                / std::sqrt(2) / (double)m_iZhouQi_bc;
//                    m_dblXW_bc = GetXiangJiao(m_dblSin_bc, m_dblCos_bc) * 60;
//               //     qDebug()<<"bcyxz:"<<m_dblYouXiaoZhi_bc;
//                    emit sigUdpDataIsOK();
//                }
//                else if (iSuanFa == 2)
//                {
//                    harmonic(m_tempBc,harmonicOrder,m_dblMaxPoint_bc*m_iZhouQi_bc,m_dblMaxPoint_bc*50,1);
//                      memcpy(m_tempBc,m_BcArray,sizeof(m_BcArray));
//                }
            //        harmonic(m_BcArray,1,m_dblMaxPoint_bc*m_iZhouQi_bc,m_dblMaxPoint_bc*50,1) ;

            if (computation_is_ok == 0)
            {
                bcIsTest = false;

                if(harmonicOrder < 5)
                    computation_is_ok = 0;
                else if(harmonicOrder < 10)
                    computation_is_ok = 3;
                else
                    computation_is_ok = harmonicOrder /3;

                harmonic(m_tempBc,harmonicOrder,m_dblMaxPoint_bc*m_iZhouQi_bc,m_dblMaxPoint_bc*50,1);

                if(testFlag==3)
                {
                    double delay2phase = 36*6*m_dblPL/10000;

                    m_dblXW_bc = m_dblXW_bc + (delay_ft3_in_packet - delay_ft3_zero)*delay2phase;

                    if(m_dblXW_bc > 20000) m_dblXW_bc -= 21600;

                    m_dblXW_bc += 1096;
                }

                emit sigUdpDataIsOK();

                qDebug()<<"bc, computation_is_ok";
            }
            else
            {
                computation_is_ok --;

                //qDebug()<<computation_is_ok;
            }

            m_isOK=true;
            m_dblSin_bc=0;
            m_dblCos_bc=0;
        //    emit sigUdpDataIsOK();
         //   m_iTestedZhouQi_bc=0;
            m_bSamplingPoints=false;
            m_iTestedPoints_bc = 0;
            m_iTestedZhouQi_bc=0;
        }
    }
//    if ((bzOver) && (bcOver))
//    {
//        bcOver=false;
//        bzOver=false;
//        harmonic(m_tempBz,1,m_dblMaxPoint_bc*m_iZhouQi,m_dblMaxPoint*50,0);
//        if (bcIsTest)
//            harmonic(m_tempBc,1,m_dblMaxPoint_bc*m_iZhouQi_bc,m_dblMaxPoint_bc*50,1);
//        emit sigUdpDataIsOK();

//    }
}

void UdpClient::UdpSend(QString s)
{
   /*
    QString s("hello");
    QByteArray cstr = s.toLatin1();
    */
    QByteArray datagram = s.toLatin1();
    udpSocket->writeDatagram(datagram.data(),datagram.size(), QHostAddress::Broadcast,port);
}

void UdpClient::UdpSendByte(char *ch,int size)
{
    QHostAddress hostaddr;
    //hostaddr= new QHostAddress("192.168.1.88");//主机IP
    hostaddr.setAddress("192.168.1.88");   //主机IP
    // for(int i=0;i<3;i++)
    udpSocket->writeDatagram(ch,size,hostaddr,port);//QHostAddress::Broadcast,hostaddr

    qDebug()<<QByteArray(ch).toHex()<<size;
}

void UdpClient::UdpSendCmd(char *ch,int size)
{
    QHostAddress hostaddr;
    hostaddr.setAddress("127.0.0.1");   //主机IP

    udpSocket->writeDatagram(ch,size,hostaddr,2000);//QHostAddress::Broadcast,hostaddr
}

void UdpClient::harmonic(double *s,int M,int len,int fs,int flag)   //s 所有的点 M：谐波次数（可以不用）len smrate*zhouqi fs smrate*50
{
    int p,n,j,k;
    double a,b;
    int f0;
    double y,y2,maxu;
    long k0;

    double A[40];
    double B[40];
    double C[40];

    f0=50;
    double r[len];
    memset(r,0,len);

    for(p=0;p<len;p++)
    {
        // r[p]=s[p] *(0.42-0.5*std::cos(2*PI*p/len)+0.08*std::cos(4*PI*p/len));

        r[p]=s[p] *(0.3125-0.46875*std::cos(2*PI*p/len)+0.1875*std::cos(4*PI*p/len)-0.03125*std::cos(6*PI*p/len));//FDMS_1 Window
    }

    // int tm=len/(fs/50)+4;//len/(fs/50)+4;
    int tm=len*M*50/fs+4;//M=1,tm=14;M=40,tm=404;
    //   int tm=len;
    double L[tm];
    double T[tm];
    for(p=0;p<tm;p++)
    {
        L[p]=0;
        T[p]=0;

        for(n=0;n<len;n++)
        {
            T[p]=T[p]+r[n]*std::cos(2*PI*p*n/len);
            L[p]=L[p]+r[n]*std::sin(2*PI*p*n/len);
        }
    }
    double u[tm];
    for(p=0;p<tm;p++)
    {
        u[p]=std::sqrt(L[p]*L[p]+T[p]*T[p]);
    }

    p = M -1;

    k0=std::ceil(f0*(p+1)*len/fs);
    maxu=u[k0];
    k=k0;
    for(j=k0-3;j<k0+3;j++)
    {
        if(maxu<u[j])
        {
            maxu=u[j];//first high of freq spectrum
            k=j;
        }
    }

    if(u[k-1]>u[k+1])  k=k-1;//second high of freq	spectrum

    y=u[k];
    y2=u[k+1];
    b=(y2-y)/(y2+y);
    a=3.5*b;

    A[p]=(k+a+0.5)*fs/len;//freq of signal

    B[p]=(y+y2)*(3.43612+0.85434*std::pow(a,2)+0.11871*std::pow(a,4))/len;

    if(T[k]>0)
        C[p]=std::acos(L[k]/u[k])-(a+0.5)*PI;//phase of angel,range of angel is 0-2*PI
    else if(T[k]<0)
        C[p]=2*PI-std::acos(L[k]/u[k])-(a+0.5)*PI;
    else if(T[k]==0)
    {
        if(L[k]>=0)   C[p]=-(a+0.5)*PI;
        else    C[p]=PI-(a+0.5)*PI;
    }


    if (flag==0)                //标准时
    {
        m_dblYouXiaoZhi=B[M-1]/1.4142135623;//m_dblFuZhi
        //   m_dblYouXiaoZhi=B[0]/1.4142135623/2000;
        m_dblXW=C[M-1]*180*60/PI;
        if (m_dblXW<0) m_dblXW+=360*60;
        //C[0]初相角  B[0]/1.4142135623 有效值
        //     qDebug()<<m_dblYouXiaoZhi;
        m_dblPL=A[0];
    }
    else                        //被测时
    {
        m_dblYouXiaoZhi_bc=B[M-1]/1.4142135623;
        m_dblXW_bc=C[M-1]*180*60/PI;
        if (m_dblXW_bc<0)   m_dblXW_bc+=360*60;

        if (testFlag==3)         //ft3时
        {
            m_dblXW_bc -=(int)etc_ft3.EDYS *1.08;
        }
    }
}
void UdpClient::ft3JianXi(unsigned short data[10],int freq)
{
    /*
     ft3_config[0]:数据集长度;
     ft3_config[1]:LNName(高8位) & DataSetName (低8位)
     ft3_config[2]:LDName
     ft3_config[3]:额定相电流
     ft3_config[4]:额定中性点电流
     ft3_config[5]:额定相电压
     ft3_config[6]:额定延时时间
     ft3_config[7]:状态字1
     ft3_config[8]:状态字2
     ft3_config[9]:暂时无用
    */
    etc_ft3.LNName=data[1]>>8;
    etc_ft3.DataSetName=data[1]&0X00FF;
    etc_ft3.LDName=data[2];
    etc_ft3.EDXDL=data[3];
    etc_ft3.EDZXDL=data[4];
    etc_ft3.EDXDY=data[5];
    etc_ft3.EDYS=data[6];
    etc_ft3.statusWord1=data[7];
    etc_ft3.statusWord2=data[8];
    etc_ft3.RangeFlag=(0x01<<13) & etc_ft3.statusWord1;
    etc_ft3.freq=freq;

    qDebug()<<"edys, ys"<<data[5]<<data[6]<<data[7]/160;

    delay_ft3_in_packet = data[6];
    delay_ft3_zero = data[2]/160.0;
  //  qDebug()<<etc_ft3.RangeFlag;
  //  (0x01<<5) & ((etc_ft3.statusWord1& 0XF0)>>8);
}
int UdpClient::getFt3Xs()
{
    double tempfz=1;
   // if (testFlag==3)         //ft3时
   // {
        if (m_iCTPT==0)
        {
            if (m_iFT3lx==1)            //保护用
            {
                if ((int)etc_ft3.RangeFlag>0)
                    tempfz=231;             //0x00E7
                else
                    tempfz=463;             //0x01CF
            }
            else                            //测量用
            {
                tempfz=11585;               //0X2D41
            }
        }
        else
        {
            tempfz=11585;           //0X2D41
        }
   // }
    return tempfz;
}
